package com.ruoyi.web.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.Category;
import com.ruoyi.web.domain.Teas;
import com.ruoyi.web.mapper.CategoryMapper;
import com.ruoyi.web.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 类别管理Service业务层处理
 *
 * @author xuzhisen
 * @date 2022-08-18
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> selectName(Category category) {
        return categoryMapper.selectName(category);
    }

    @Override
    public List<Category> selectCategoryName() {
        return categoryMapper.selectCategoryName();
    }

    /**
     * 查询类别管理
     *
     * @param categoryId 类别管理主键
     * @return 类别管理
     */
    @Override
    public Category selectCategoryByCategoryId(Long categoryId)
    {
        return categoryMapper.selectCategoryByCategoryId(categoryId);
    }

    /**
     * 查询类别管理列表
     *
     * @param category 类别管理
     * @return 类别管理
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增类别管理
     *
     * @param category 类别管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCategory(Category category)
    {
        int rows = categoryMapper.insertCategory(category);
        insertTeas(category);
        return rows;
    }

    /**
     * 修改类别管理
     *
     * @param category 类别管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCategory(Category category)
    {
        categoryMapper.deleteTeasByCategoryId(category.getCategoryId());
        insertTeas(category);
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除类别管理
     *
     * @param categoryIds 需要删除的类别管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCategoryByCategoryIds(Long[] categoryIds)
    {
        categoryMapper.deleteTeasByCategoryIds(categoryIds);
        return categoryMapper.deleteCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除类别管理信息
     *
     * @param categoryId 类别管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCategoryByCategoryId(Long categoryId)
    {
        categoryMapper.deleteTeasByCategoryId(categoryId);
        return categoryMapper.deleteCategoryByCategoryId(categoryId);
    }

    /**
     * 新增茶叶管理信息
     *
     * @param category 类别管理对象
     */
    public void insertTeas(Category category)
    {
        List<Teas> teasList = category.getTeasList();
        Long categoryId = category.getCategoryId();
        if (StringUtils.isNotNull(teasList))
        {
            List<Teas> list = new ArrayList<Teas>();
            for (Teas teas : teasList)
            {
                teas.setCategoryId(categoryId);
                list.add(teas);
            }
            if (list.size() > 0)
            {
                categoryMapper.batchTeas(list);
            }
        }
    }
}
